package com.numberone.master.modules.rsa.service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.numberone.master.common.service.CrudService;
import com.numberone.master.common.utils.StringUtils;
import com.numberone.master.modules.rsa.dao.RsaKeyDao;
import com.numberone.master.modules.rsa.entity.RsaKey;

/**
 * Rsa密匙Service
 * @author chengkang
 * @version 2018-02-23
 */
@Service
@Transactional(readOnly = true)
public class RsaService extends CrudService<RsaKeyDao, RsaKey> {
	private  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
	private  AtomicInteger atoInt = new AtomicInteger(1);
	@Autowired
	private RsaKeyDao rsaKeyDao;
	
	@Transactional(readOnly = false)
	public RsaKey selectRsaKey(String userId){
		return rsaKeyDao.selectRsaKey(userId);
	}
	
	@Transactional(readOnly = false)
	public  String getTransid() {
		String transid = "NM8A1HL";
		String datetime = sdf.format(new Date());
		transid += datetime;
		int num = 0;
		synchronized (this) {
			num = atoInt.getAndIncrement();
			if(num>99999998){
				atoInt.set(1);
			}
		}
		transid +=StringUtils.leftPad(String.valueOf(num), 8, "0");
		return transid;
	}
}